extern crate fejpsl_contract;

use fejpsl_contract::follow::Follow;
use fejpsl_contract::follow::{FollowListData, FollowPageData};

pub async fn list_my_follow(page_no:&i32,user_uuid:&String) -> FollowPageData {
    let client = super::db::connect().await.unwrap();
    let mut vec_follow = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset= *page_no as i64 *per_page_num;
    let rows = &client.query("select a.user_uuid,a.follow_uuid,b.nice_name,b.real_name,b.is_explicit,
        b.avatar_text from t_follow_me a,t_user b
        where a.user_uuid=$3
	    and a.is_block=false and a.follow_uuid=b.user_uuid::character varying order by a.id desc limit $1 offset $2",
                             &[&per_page_num,&offset,&user_uuid]).await.unwrap();
    for row in rows {
        let mut m_real_name=String::from("");
        let tmp_str: Option<String> = row.get(3);
        if let Some(_i) = tmp_str {m_real_name= row.get(3); }else{ m_real_name=String::from("") }
        let mut m_avatar_text=String::from("");
        let tmp_str: Option<String> = row.get(5);
        if let Some(_i) = tmp_str {m_avatar_text= row.get(5); }else{ m_avatar_text=String::from("") }
        let follow = Follow {
            user_uuid:   row.get(0),
            follow_uuid: row.get(1),
            nice_name:  row.get(2),
            real_name:   m_real_name,
            is_explicit:   row.get(4),
            avatar_text: m_avatar_text,
        };
        vec_follow.push(follow);
    }
    let total=rows.len() as i32;
    let page_count=(total/10) as i32;
    let mut over=false;
    if page_no>=&page_count {over=true}
    let size=per_page_num as i32;
    let follow_list_data= FollowListData {
        cur_page:*page_no,
        datas:Some(vec_follow),
        offset: offset,
        over: over,
        page_count:page_count,
        size:size,
        total:total,
    };
    let follow_page_data= FollowPageData {
        data:follow_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    return follow_page_data;
}
pub async fn list_follow_me(page_no:&i32,user_uuid:&String) -> FollowPageData {
    let client = super::db::connect().await.unwrap();
    let mut vec_follow = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset= *page_no as i64 *per_page_num;
    let rows = &client.query("select a.user_uuid,a.follow_uuid,b.user_name,b.real_name,b.is_explicit,
    b.avatar_text from t_follow_me a,t_user b
    where a.follow_uuid=$3
	and a.is_block=false and a.follow_uuid=b.user_uuid::character varying order by a.id desc limit $1 offset $2",
                             &[&per_page_num,&offset,&user_uuid]).await.unwrap();
    for row in rows {
        let mut m_real_name = String::from("");
        let tmp_str: Option<String> = row.get(3);
        if let Some(_i) = tmp_str { m_real_name = row.get(3); } else { m_real_name = String::from("") }
        let mut m_avatar_text = String::from("");
        let tmp_str: Option<String> = row.get(5);
        if let Some(_i) = tmp_str { m_avatar_text = row.get(5); } else { m_avatar_text = String::from("") }
        let follow = Follow {
            user_uuid: row.get(0),
            follow_uuid: row.get(1),
            nice_name: row.get(2),
            real_name: m_real_name,
            is_explicit: row.get(4),
            avatar_text: m_avatar_text,
        };
        vec_follow.push(follow);
    }
        let total=rows.len() as i32;
        let page_count=(total/10) as i32;
        let mut over=false;
        if page_no>=&page_count {over=true}
        let size=per_page_num as i32;
        let follow_list_data= FollowListData {
            cur_page:*page_no,
            datas:Some(vec_follow),
            offset: offset,
            over: over,
            page_count:page_count,
            size:size,
            total:total,
        };
        let follow_page_data= FollowPageData {
            data:follow_list_data,
            error_code: 0,
            error_msg:"Success.".to_string(),
        };
        return follow_page_data;
}

pub async fn list_block_people(page_no:&i32,user_uuid:&String) -> FollowPageData {
    let client = super::db::connect().await.unwrap();
    let mut vec_follow = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset= *page_no as i64 *per_page_num;
    let rows = &client.query("select a.user_uuid,a.follow_uuid,b.nice_name,b.real_name,b.is_explicit,
        b.avatar_text from t_follow_me a,t_user b where a.user_uuid=$3
	    and a.is_block=true and a.follow_uuid=b.user_uuid::character varying order by a.id desc limit $1 offset $2",
                             &[&per_page_num,&offset,&user_uuid]).await.unwrap();
    for row in rows {
        let mut m_real_name=String::from("");
        let tmp_str: Option<String> = row.get(3);
        if let Some(_i) = tmp_str {m_real_name= row.get(3); }else{ m_real_name=String::from("") }
        let mut m_avatar_text=String::from("");
        let tmp_str: Option<String> = row.get(5);
        if let Some(_i) = tmp_str {m_avatar_text= row.get(5); }else{ m_avatar_text=String::from("") }
        let follow = Follow {
            user_uuid:   row.get(0),
            follow_uuid: row.get(1),
            nice_name:  row.get(2),
            real_name:   m_real_name,
            is_explicit:   row.get(4),
            avatar_text: m_avatar_text,
        };
        vec_follow.push(follow);
    }
    let total=rows.len() as i32;
    let page_count=(total/10) as i32;
    let mut over=false;
    if page_no>=&page_count {over=true}
    let size=per_page_num as i32;
    let follow_list_data= FollowListData {
        cur_page:*page_no,
        datas:Some(vec_follow),
        offset: offset,
        over: over,
        page_count:page_count,
        size:size,
        total:total,
    };
    let follow_page_data= FollowPageData {
        data:follow_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    return follow_page_data;
}
